<template>

  <scroller class="scroller"
            :show-scrollbar="false">

    <vm-cell label="用工总数"
             :hasTopBorder="true">
      <input slot="title"
             placeholder="用工总数"
             v-model="params.emp_total"
             type="number"
             class="input" />
    </vm-cell>

    <vm-cell label="常用工人">
      <input slot="title"
             placeholder="常用工人数量"
             v-model="params.emp_com_used"
             type="number"
             class="input" />
    </vm-cell>

    <vm-cell label="临时工人">
      <input slot="title"
             placeholder="临时工人数量"
             v-model="params.emp_temp"
             type="number"
             class="input" />
    </vm-cell>

    <vm-cell label="贫困户数">
      <input slot="title"
             placeholder="贫困户数"
             v-model="params.poverty_households"
             type="number"
             class="input" />
    </vm-cell>

    <vm-cell label="近一年人工支出">
      <input slot="title"
             placeholder="近一年人工支出"
             v-model="params.year_emp_exp"
             type="number"
             class="input" />
    </vm-cell>

    <vm-cell label="备注">
      <input slot="title"
             placeholder="备注"
             v-model="params.remark"
             class="input" />
    </vm-cell>

    <vm-button text="提交"
               class="mt-3"
               :disabled="disabled"
               @buttonClicked="submit"></vm-button>
  </scroller>

</template>
<script>
import VmButton from '../../components/vm-button.vue'
import VmCell from '../../components/vm-cell.vue'
import {
  STORAGE_FARM
} from '../../plugins/constant'

export default {
  components: { VmButton, VmCell },
  props: {},
  data() {
    return {
      farm: {},
      params: {
        customer_id: '',
        customer_related_id: '',
        emp_com_used: '',
        emp_temp: '',
        emp_total: '',
        poverty_households: '',
        year_emp_exp: '',
        remark: ''
      },
      id: '',
      disabled: false
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getParams()
  },
  methods: {
    getParams() {
      this.farm = this.$storage.getSync(STORAGE_FARM) || {}
      this.$router.getParams().then(res => {
        const data = res || {}
        Object.keys(this.params).forEach(key => {
          this.params[key] = data[key] || ''
        })
        this.getEmp()
      })
    },
    async getEmp() {
      if (!this.farm.id) { return }
      this.$notice.loading.show()
      const [res] = await this.$utils.pm(this.$api.getData({
        server: 'credit',
        table: 'datum_customer_emp',
        params: {
          'customer_id.eq': this.farm.id
        },
        errorMsg: '获取数据失败'
      }))
      this.$notice.loading.hide()
      if (!res) { return }
      const data = res && res.length && res[0] || {}
      this.id = data.datum_customer_emp_id || ''
      Object.keys(this.params).forEach(key => {
        this.params[key] = data[key] || ''
      })
    },
    submit() {
      if (!this.params.emp_total) {
        return this.$notice.toast({ message: '请输入用工总数' })
      }
      if (this.id) {
        this.putData()
      } else {
        this.postData()
      }
    },
    async putData() {
      const [res] = await this.$utils.pm(this.$api.updateData({
        server: 'credit',
        table: 'datum_customer_emp',
        id: this.id,
        data: this.params,
        errorMsg: '提交失败，请重试'
      }))
      if (!res) { return }
      this.$notice.toast({ message: '提交成功' })
      this.$router.back()
    },
    async postData() {
      const [res] = await this.$utils.pm(this.$api.addData({
        server: 'credit',
        table: 'datum_customer_emp',
        data: this.params,
        errorMsg: '提交失败，请重试'
      }))
      if (!res) { return }
      this.$notice.toast({ message: '提交成功' })
      this.$router.back()
    }
  }
}
</script>
<style lang="scss" scoped>
@import "../../css/variables.scss";

.mt-3 {
  margin-top: 30px;
}
.input {
  flex: 1;
  height: 60px;
  font-size: 30px;
  tint-color: #999;
  text-align: right;
}
</style>
